
import os
from selenium import webdriver
from selenium.webdriver.common.by import By
import time

# 获取默认的下载文件夹路径
download_path = os.path.join(os.path.expanduser("~"), "Downloads")

# 输出下载路径，检查是否正确
print(f"下载路径是: {download_path}")

# 文件保存路径
file_path = os.path.join(download_path, "file_links.txt")

# 初始化浏览器
driver = webdriver.Chrome()  # 确保已安装 ChromeDriver
url = "https://sid.erda.dk/cgi-sid/ls.py?share_id=GxrFNGtykS"
driver.get(url)

# 等待页面加载
time.sleep(5)

# 抓取所有文件链接
links = driver.find_elements(By.TAG_NAME, "a")

# 输出抓取到的链接数量
print(f"抓取到的链接数量: {len(links)}")

# 如果没有链接，则打印错误
if len(links) == 0:
    print("没有找到文件链接，检查页面是否加载完整，或者文件链接是否被动态生成。")
else:
    # 将链接保存到默认下载文件夹
    with open(file_path, "w") as f:
        for link in links:
            href = link.get_attribute("href")
            if href and (".hic" in href or ".assembly" in href or ".fasta" in href):
                f.write(href + "\n")

    print(f"文件链接已保存到 {file_path}")

# 关闭浏览器
driver.quit()
